package edu.maplef.bookwild.schema;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

/**
 * 用于本地的图书数据，需要兼顾豆瓣和GAE两方 
 * (1)保证GAE能够使用JDO存取数据 
 * (2)保证豆瓣抓取的信息能够迅速转换
 * 
 * @author maple
 */

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Book extends Base {
	/**
	 * 图书标识，形如 http://api.douban.com/book/subject/5391812
	 */
	@PrimaryKey
	private String id;

	@Persistent
	private String title;

	@NotPersistent
	private String isbn10;

	@NotPersistent
	private String isbn13;

	@NotPersistent
	private String pages;

	@NotPersistent
	private String translator;

	@NotPersistent
	private String price;

	@Persistent
	private List<String> author;

	@Persistent
	private String publisher;

	@NotPersistent
	private String infolink;

	@NotPersistent
	private String imagelink;

	@NotPersistent
	private String pubdate;

	public Book() {
	}

	public Book(String title, String author, String publisher) {
		this.setTitle(title);
		this.addAuthor(author);
		this.setPublisher(publisher);
	}

	// getters and setters
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getIsbn10() {
		return isbn10;
	}

	public void setIsbn10(String isbn10) {
		this.isbn10 = isbn10;
	}

	public String getIsbn13() {
		return isbn13;
	}

	public void setIsbn13(String isbn13) {
		this.isbn13 = isbn13;
	}

	public String getPages() {
		return pages;
	}

	public void setPages(String pages) {
		this.pages = pages;
	}

	public String getTranslator() {
		return translator;
	}

	public void setTranslator(String translator) {
		this.translator = translator;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public List<String> getAuthor() {
		return author;
	}

	public void setAuthor(List<String> author) {
		this.author = author;
	}

	/**
	 * add user to the list without check  
	 * @param p the name of the person
	 */
	public void addAuthor(String p) {
		if (author == null)
			author = new ArrayList<String>();
		author.add(p);
	}

	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	public String getInfolink() {
		return infolink;
	}

	public void setInfolink(String infolink) {
		this.infolink = infolink;
	}

	public String getImagelink() {
		return imagelink;
	}

	public void setImagelink(String imagelink) {
		this.imagelink = imagelink;
	}

	public String getPubdate() {
		return pubdate;
	}

	public void setPubdate(String pubdate) {
		this.pubdate = pubdate;
	}

	/**
	 * 将book对象转化为字符串
	 * @param mid 每个字段之间的间隔内容，如"-":title-author
	 * @return 转化后的字符串
	 */
	public String toString(String mid) {
		String s = "unknown";
		if (author != null)
			s = author.get(0);
		return title + mid + s + mid + publisher + mid + pubdate;
	}

	public String toString() {
		return toString("-");
	}
}